﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using kad2SimSoft.Resources.Lokalisierung;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für XMLImportControl.xaml
    /// </summary>
    public partial class XMLImportControl : System.Windows.Controls.UserControl
    {
        String pathToXML;
        String selectedLanguage = "";

        public XMLImportControl()
        {
            InitializeComponent();            
        }

        private void BrowseXMLFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = "C:\\";

            DialogResult result = dialog.ShowDialog();
            if (!dialog.FileName.Equals(""))
            {
                if (dialog.FileName.Contains("result") && dialog.FileName.Contains("xml"))
                {
                    pathToXML = dialog.FileName;
                    imgCheck.Visibility = System.Windows.Visibility.Visible;
                    bt_SelectXML.IsEnabled = false;
                    bt_ImportXMLFile.IsEnabled = true;
                }
                else
                {
                    System.Windows.MessageBox.Show(kad2SimSoft.Properties.Resources.msgWrongInputXml);
                }
            }
        }

        private void bt_ImportXMLFile_Click(object sender, RoutedEventArgs e)
        {
            kad2SimSoft.Properties.Settings.Default["XMLImportPath"] = pathToXML;
            kad2SimSoft.Properties.Settings.Default.Save();
            Xml xml = new Xml(true, FindPageControl(this));

            FindPageControl(this).ActPeriod = xml.ActualPeriod;
            FindPageControl(this).PurchasedParts = xml.PurchasedParts;
            FindPageControl(this).ManufacturedParts = xml.ManufacturedParts;
            FindPageControl(this).Endproducts = xml.Endproducts;
            FindPageControl(this).Orders = xml.Orders;
            FindPageControl(this).OrdersInWork = xml.OrdersInWork;
            FindPageControl(this).WaitinglistStock = xml.WaitinglistStock;
            FindPageControl(this).Keydata = xml.Keydata;
            //FindPageControl(this).WaitinglistWorkplace = xml.WaitinglistWorkplace;
            FindPageControl(this).Workplaces = xml.Workplaces;
            FindPageControl(this).setButtonsDisable();
            FindPageControl(this).XmlImportControl = null;
            FindPageControl(this).ForecastControl = null;
            FindPageControl(this).ProducionPlanningView = null;
            FindPageControl(this).PurchasingView = null;
            FindPageControl(this).WarehouseView = null;
            FindPageControl(this).CapacityPlanView = null;
            FindPageControl(this).ProductionOrderView = null;
            FindPageControl(this).setButtonsEnabledImported();
            FindPageControl(this).fillProductDescriptions();
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.FORECAST);
        }

        public static MainWindow FindPageControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            MainWindow page = parent as MainWindow;
            if (page != null)
            {
                return page;
            }
            else
            {
                return FindPageControl(parent);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            selectedLanguage = kad2SimSoft.Properties.Settings.Default["SelectedLanguage"].ToString();
            if (selectedLanguage.Contains("de") || selectedLanguage.Equals("") || selectedLanguage == null)
            {
                FindPageControl(this).SelectedLanguage = "de-DE";
                rbGerman.IsChecked = true;
                rbEnglish.IsChecked = false;
                rbfrench.IsChecked = false;
                rbspanish.IsChecked = false;
                rbrussia.IsChecked = false;
                rbchinese.IsChecked = false;
            }
            if (selectedLanguage.Contains("en"))
            {
                FindPageControl(this).SelectedLanguage = "en";
                rbGerman.IsChecked = false;
                rbEnglish.IsChecked = true;
                rbfrench.IsChecked = false;
                rbspanish.IsChecked = false;
                rbrussia.IsChecked = false;
                rbchinese.IsChecked = false;
            }
            if (selectedLanguage.Contains("es")) {
                FindPageControl(this).SelectedLanguage = "es";
                rbGerman.IsChecked = false;
                rbEnglish.IsChecked = false;
                rbfrench.IsChecked = false;
                rbspanish.IsChecked = true;
                rbrussia.IsChecked = false;
                rbchinese.IsChecked = false;
            }
            if (selectedLanguage.Contains("fr")) {
                FindPageControl(this).SelectedLanguage = "fr";
                rbGerman.IsChecked = false;
                rbEnglish.IsChecked = false;
                rbfrench.IsChecked = true;
                rbspanish.IsChecked = false;
                rbrussia.IsChecked = false;
                rbchinese.IsChecked = false;
            }
            if (selectedLanguage.Contains("ru")) {
                FindPageControl(this).SelectedLanguage = "ru";
                rbGerman.IsChecked = false;
                rbEnglish.IsChecked = false;
                rbfrench.IsChecked = false;
                rbspanish.IsChecked = false;
                rbrussia.IsChecked = true;
                rbchinese.IsChecked = false;
            }
            if (selectedLanguage.Contains("zh-HK")) {
                FindPageControl(this).SelectedLanguage = "zh-HK";
                rbGerman.IsChecked = false;
                rbEnglish.IsChecked = false;
                rbfrench.IsChecked = false;
                rbspanish.IsChecked = false;
                rbrussia.IsChecked = false;
                rbchinese.IsChecked = true;
            }

        }

        private void Button_Language_Click(object sender, RoutedEventArgs e)
        {
            if (rbGerman.IsChecked.Value)
            {
                FindPageControl(this).SelectedLanguage = "de-DE";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "de-DE";

            }
            if (rbEnglish.IsChecked.Value)
            {
                FindPageControl(this).SelectedLanguage = "en";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "en";
            }
            if (rbfrench.IsChecked.Value) {
                FindPageControl(this).SelectedLanguage = "fr";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "fr";
            }
            if (rbspanish.IsChecked.Value) {
                FindPageControl(this).SelectedLanguage = "es";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "es";
            }
            if (rbrussia.IsChecked.Value) {
                FindPageControl(this).SelectedLanguage = "ru";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "ru";
            }
            if (rbchinese.IsChecked.Value) {
                FindPageControl(this).SelectedLanguage = "zh-HK";
                kad2SimSoft.Properties.Settings.Default["SelectedLanguage"] = "zh-HK";
            }

            Properties.Settings.Default.Save();
            System.Threading.Thread.Sleep(1500);
            System.Windows.Forms.Application.Restart();
            System.Windows.Application.Current.Shutdown();
            //System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
            //Application.Current.Shutdown();
        }
    }
}
